package com.stxx.stiot2.service.impl;


import cn.hutool.core.util.StrUtil;
import com.stxx.framwork.constants.FluxDataType;
import com.stxx.framwork.constants.LogicType;
import com.stxx.framwork.domain.model.PageQuery;
import com.stxx.framwork.domain.model.PageResult;
import com.stxx.framwork.utils.FluxUtils;
import com.stxx.framwork.utils.StoreUtils;
import com.stxx.stiot2.constants.RedisKeys;
import com.stxx.stiot2.domain.Alarm;
import com.stxx.stiot2.domain.Signal;
import com.stxx.stiot2.external.RedisServer;
import com.stxx.stiot2.service.SignalService;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Init;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.sql.SqlBuilder;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author Fuzhenlei
 * @date 2025/01/15 16:41
 */
@Component
public class SignalServiceImpl implements SignalService {

    @Init
    public void start(){
        this.configLogic(Signal::getName, LogicType.LIKE);
    }

    @Inject
    StoreUtils storeUtils;


    @Inject
    RedisServer redisServer;

    @Override
    public Signal queryById(String id) {
        try {
            return storeUtils.selectById(id, Signal.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Init
    public void init(){
        configLogic(Signal::getName, LogicType.LIKE);
    }

    @Override
    public int insert(Signal bean) throws SQLException {
        validEntityBeforeSave(bean);
        return storeUtils.insert(bean);
    }

    @Override
    public int update(Signal bean) throws SQLException {
        validEntityBeforeSave(bean);
        redisServer.delete(bean.getCode());
        return storeUtils.update(bean);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Signal bean){

    }




    @Override
    public int delete(String name) throws SQLException {
        redisServer.delete(name);
        return storeUtils.delete(Signal.class,name);
    }

    @Override
    public int deleteByIds(String[] ids) throws SQLException {
        redisServer.delete(ids);
        return storeUtils.deleteByIds(Signal.class,ids);
    }

    @Override
    public List<Signal> list(Signal obj) throws SQLException {
        return storeUtils.selectList(Signal.class,obj);
    }

    @Override
    public PageResult<Signal> pageList(Signal obj, PageQuery pageQuery) throws SQLException {
        pageQuery.setOrderByColumn("category");
        return storeUtils.selectPageList(Signal.class,obj,pageQuery);
    }



    @Override
    public PageResult<Signal> listBySw(String sw,PageQuery pageQuery) throws SQLException {
        SqlBuilder sb = new SqlBuilder();
        sb.append("from ").append(StoreUtils.getTableName("signal"))
                .append(" where code like concat('%',?,'%')",sw)
                .append(" or name like concat('%',?,'%')",sw);
        sb.backup();
        sb.insert("select * ");
        if(StrUtil.isNotEmpty(pageQuery.getOrderByColumn())){
            if(StrUtil.isBlank(pageQuery.getIsAsc())){
                pageQuery.setIsAsc("asc");
            }
            sb.append(" order by ")
                    .append(pageQuery.getOrderByColumn())
                    .append(" ")
                    .append(pageQuery.getIsAsc());
        }
        sb.append(" limit ?,? ", (pageQuery.getPageNum()-1)*pageQuery.getPageSize(),pageQuery.getPageSize()); //分页获取列表

        List<Signal> list = storeUtils.sql(sb).queryRowList(Signal.class);
        //回滚（可以复用备份前的代码构建）
        sb.restore();
        sb.insert("select count(*)  ");
        //查询总数
        Long total = Long.parseLong(storeUtils.sql(sb).queryValue().toString());
        PageResult<Signal> pageResult = new PageResult(list,total);
        return pageResult;
    }
}
